﻿Imports System.Windows.Forms

Public Class frmParameters
    Private ReportInfo As WRDB.Reports.RdlViewer.clsReportInfo = Nothing
    Private Project As WRDB.Project.clsProject = Nothing

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        'retrieve the values specified by the user
        For i As Integer = 0 To ReportInfo.ReportParameters.Count - 1
            ReportInfo.ReportParameters(i).Value = tblParms.GetControlFromPosition(1, i).Text
        Next

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmParameters_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        With tblParms
            .SuspendLayout()
            .RowCount = 0
            .RowStyles.Clear()
            .CellBorderStyle = TableLayoutPanelCellBorderStyle.Single
            .GrowStyle = TableLayoutPanelGrowStyle.AddRows
        End With
        For Each rp As Reports.RdlViewer.ReportParmInfo In ReportInfo.ReportParameters
            Dim cntl As Control = Nothing
            Select Case rp.DataType
                Case "String", "Integer"
                    If rp.Datasets.Count + rp.Parameters.Count = 0 Then 'just use text box
                        cntl = New TextBox
                        If rp.MultiValue Then
                            With CType(cntl, TextBox)
                                .Multiline = True
                                .ScrollBars = ScrollBars.Both
                                .WordWrap = False
                                .Height *= 5
                                .AcceptsReturn = True
                            End With
                        End If
                    Else 'use combobox
                        Dim cbo As New ComboBox
                        cbo.DropDownStyle = ComboBoxStyle.DropDownList
                        For Each ds As WRDB.Reports.RdlViewer.ParmDatasetInfo In rp.Datasets
                            For Each kv As KeyValuePair(Of String, String) In ds.fields
                                If ReportInfo.DataSets(ds.datasetName).dataTable Is Nothing Then
                                    'ReportInfo.DataSets(ds.datasetName).dataTable = ReportInfo.GetData(ds.datasetName, Nothing)
                                    ReportInfo.DataSets(ds.datasetName).dataTable = ReportInfo.GetData(ds.datasetName, Nothing, Project)
                                End If
                                Dim view As New DataView(ReportInfo.DataSets(ds.datasetName).dataTable)
                                Dim dt As DataTable = view.ToTable(True, kv.Key)
                                For Each dr As DataRow In dt.Rows
                                    cbo.Items.Add(dr(kv.Key))
                                Next
                            Next
                        Next
                        For Each kv As KeyValuePair(Of String, String) In rp.Parameters
                            cbo.Items.Add(kv.Key)
                        Next
                        cntl = cbo
                    End If
                Case "DateTime"
                    cntl = New DateTimePicker
                    With CType(cntl, DateTimePicker)
                        .Format = DateTimePickerFormat.Short
                    End With
            End Select
            cntl.Text = rp.Value 'set to default value

            Dim lbl As New Label
            With lbl
                .Text = rp.Prompt
                If Not .Text.EndsWith(":") Then .Text &= ":"
                .AutoSize = True
                .Anchor = AnchorStyles.Right
            End With
            With cntl
                .Dock = DockStyle.Fill
                If rp.Defaults.Count = 1 Then .Text = rp.Defaults(0).ToString
            End With
            AddRow(lbl, cntl)
        Next
        tblParms.ResumeLayout()
    End Sub

    Private Sub AddRow(label As Control, value As Control)
        tblParms.RowCount += 1
        Dim rowIndex As Integer = tblParms.RowCount - 1
        'Dim rowIndex As Integer = AddTableRow()
        tblParms.Controls.Add(label, 0, rowIndex)
        tblParms.Controls.Add(value, 1, rowIndex)
    End Sub

    Private Function AddTableRow() As Integer
        Dim index As Integer = tblParms.RowCount + 1
        Dim style As RowStyle = New RowStyle(SizeType.AutoSize)
        tblParms.RowStyles.Add(style)
        Return index
    End Function

    Friend Sub New(Project As WRDB.Project.clsProject, ByRef ReportInfo As WRDB.Reports.RdlViewer.clsReportInfo)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.ReportInfo = ReportInfo
        Me.Project = Project
    End Sub
End Class
